﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sylvain.Manager;
using System.Net.Mail;
using System.Net;

public partial class Contacts : System.Web.UI.Page
{
    /// <summary>
    ///  Display Page Content
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            //ISylvainManager objManager = new SylvainManager();
            //int pageId = objManager.GetPageId("contact_us");
            //string pageContent = objManager.GetPageContent(pageId);
            //if (pageContent.Length > 0)
            //{

            //    mainDiv.InnerHtml = pageContent;
            //}
        }
    }
    /// <summary>
    /// Send Mail
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            //SMTP Configuration for Hotmail
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["UserName"].ToString(), System.Configuration.ConfigurationManager.AppSettings["Password"].ToString());
            smtpClient.Host = System.Configuration.ConfigurationManager.AppSettings["Host"].ToString();
            smtpClient.Port = 25;
            smtpClient.EnableSsl = true;

            MailMessage message = new MailMessage();
            message.From = new MailAddress(txtEmail.Text);
            message.To.Add(new MailAddress(System.Configuration.ConfigurationManager.AppSettings["ToEmail"].ToString()));
            message.Subject = "Query";
            message.IsBodyHtml = false;
            string name=txtName.Text;
            message.Body = "Hello Sir/Madam, " + "\n\n" + txtMessage.Text + "\n\n" + "Regards" + "\n" + txtName.Text + "\n" + "Email: " + txtEmail.Text + "\nPhone: " + txtPhone.Text;
            smtpClient.Send(message);
           ShowAlert("Email has been sent.");
           ResetControls(); 
        }
        catch(Exception ex)
        {
            ShowAlert("Unable to send e-mail. Please try again.");
        }
    }

    private void ShowAlert(string message)
    {
        // Cleans the message to allow single quotation marks 
        string cleanMessage = message.Replace("'", "\'");
        string script = @"<script type='text/javascript'>setTimeout(""alert('" + cleanMessage + "')";
        script += @""",500);</script>";

        // Gets the executing web page 
        Page page = HttpContext.Current.CurrentHandler as Page;

        // Checks if the handler is a Page and that the script isn't allready on the Page 
        if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
        {
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script);
        }
    }

    /// <summary>
    /// Reset Controls.
    /// </summary>
    private void ResetControls()
    {
        txtEmail.Text = string.Empty;
        txtName.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtMessage.Text = string.Empty;

    }

    protected void btnGetDirections_Click(object sender, ImageClickEventArgs e)
    {
       
    }
}